package com.typy.www.tpay.ui.acitivity;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.typy.www.tpay.MainActivity;
import com.typy.www.tpay.R;
import com.typy.www.tpay.base.BaseBackActivity;
import com.typy.www.tpay.bean.bean.BaseReturn;
import com.typy.www.tpay.bean.listvm.NoticeListVm;
import com.typy.www.tpay.bean.vm.NoticeVm;
import com.typy.www.tpay.constance.Constance;
import com.typy.www.tpay.ui.adapter.NoticeAdapter;
import com.typy.www.tpay.ui.prt.PtrListContent;
import com.typy.www.tpay.util.GsonUtils;
import com.typy.www.tpay.util.Logutil;
import com.typy.www.tpay.util.ParamsUtil;
import com.typy.www.tpay.util.UiHelper;
import com.typy.www.tpay.util.listviewanimations.IntentUtil;
import com.typy.www.tpay.volley.HTTPUtils;
import com.typy.www.tpay.volley.VolleyListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by 13799 on 2018/6/14.
 */

public class NoticeActivity extends BaseBackActivity {

    private ListView lvRoot;
    private NoticeAdapter adapter;
    private NoticeListVm mListVm;
    private List<NoticeVm> mData = new ArrayList<>();
    private PtrListContent ptrListContent;

    public static void show(Context context) {
        IntentUtil.intent2Activity(context, NoticeActivity.class);
    } public static void show(Context context,int classType) {
        Intent intent=new Intent();
        intent.putExtra("classType",classType);
        IntentUtil.intent2Activity(context, NoticeActivity.class);
    }

    @Override
    protected void initView() {
        super.initView();
        ButterKnife.bind(this);
        setTitle("公告");
        getData(1, false);
        initAdapter();
    }
//    initTitle


    private void initAdapter() {
        lvRoot = ptrListContent.getListView();
        adapter = new NoticeAdapter(getActivity());
        lvRoot.setAdapter(adapter);
    }


    @Override
    protected View getContentView() {
        ptrListContent = new PtrListContent(getActivity()) {
            @Override
            protected void getPage(int pageIndex, boolean refresh) {
                getData(pageIndex, refresh);
            }
        };
        return ptrListContent.getmView();
    }

    private void getData(int page, final boolean refresh) {
        ParamsUtil paramsUtil = new ParamsUtil();
        paramsUtil.put("pageNum", page);
        paramsUtil.put("pageSize", Constance.LIMT);

        HTTPUtils.post(getActivity(), Constance.getNoticelistURL(), paramsUtil.getParams(), new VolleyListener() {
            @Override
            public void onResponse(BaseReturn ret) {
                if (ret.getCode() == 200 && ret.getData() != null) {
                    if (refresh)
                        mData.clear();
                    mListVm = GsonUtils.parseJSON(ret.getData().toString(), NoticeListVm.class);
                    if (mListVm != null && mListVm.getData() != null && mListVm.getData().size() != 0) {
                        mData.addAll(mListVm.getData());
                        adapter.setData(mData);
                        adapter.notifyDataSetChanged();
                    } else {
                        UiHelper.showToast(getActivity(), "暂无公告");
                    }
                    ptrListContent.loadInitialPage(mListVm.getCount());
                }
            }

            @Override
            public void onResponseErr(int code, String msg) {
                UiHelper.showToast(getActivity(), msg);
            }
        });
    }


}
